package com.ai.bitcoin.controller;

import com.ai.bitcoin.annotation.Privilege;
import com.ai.bitcoin.input.GeneralRequest;
import com.ai.bitcoin.input.RequestHepler;
import com.ai.bitcoin.jpa.dao.BccUserDao;
import com.ai.bitcoin.jpa.entity.BccUser;
import com.ai.bitcoin.service.RedeemCodeService;
import com.ai.bitcoin.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@RequestMapping("/redeem-code")
@Slf4j
@Controller
public class RedeemController {

    @Autowired
    private RedeemCodeService redeemCodeService;

    @Autowired
    private BccUserDao bccUserDao;


    /**
     * 新建一定数量的兑换码
     */
    @GetMapping("genCodeByExcel")
    public String genCodeByExcel() throws Exception {
        return "uploadXls";
    }

    /**
     * 新建一定数量的兑换码
     */
    @PostMapping("genCodeByExcel")
    public ResponseEntity<byte[]> genCodeByExcel(@RequestBody MultipartFile file) throws Exception {
        return redeemCodeService.createByExcel(file.getInputStream());
    }


    /**
     * 新建一定数量的兑换码
     * @param dto
     * @return
     */
//    @PutMapping
//    public String create(@RequestBody RedeemInputDto dto) {
//        return JacksonUtils.toJson(redeemCodeService.create(dto));
//    }


    /**
     * 执行兑换
     *
     * @param request
     * @param code
     * @return
     */
    @Privilege
    @RequestMapping(value = "/exchange", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String exchange(HttpServletRequest request, @RequestParam String code) {
        GeneralRequest generalRequest = RequestHepler.processRequest(request);
        BccUser user = generalRequest.getUser();
        return JacksonUtils.toJson(redeemCodeService.exchange(code, user));
    }

}
